﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Fluid.Controls;
using TouchRC.Classes;
using System.Drawing;
using TouchRC.Logic;
using TouchRC.Themes;
using TouchRC.BarManagers;
using System.Windows.Forms;
using TouchRC.Custom_Controls;

namespace TouchRC
{
    public abstract class FluidPanelBase : FluidPanel
    {
        #region Abstract Methods

        public abstract void SetControlPositions(ScreenOrientation screenOrientation);
        public abstract void RefreshValues();
        public abstract void AddControls();
        public abstract void InitPanelControls();
        public abstract List<BarOption> GetBarOptions();
        public abstract void Back();

        #endregion

        #region Private Members

        private Bitmap m_BackgroundImage;
        private Image m_BackgroundImageLandscape;
        private ClickableLabel m_Header;
        private string m_HeaderText = string.Empty;
        private BarManager m_BarManager;
        private FluidButton m_MenuButton;
        private Timer m_Timer;

        private bool m_ShowBackground = true;
        private bool m_EnableRemoteControlGesture = true;
        private bool m_EnableNowPlayingGesture = true;
        private bool m_ShowMenuButton = true;
        private bool m_ShowHeader = true;

        #endregion

        #region Public Accessors

        public int ScreenWidth { get { return (int)(AppSettings.ScreenResolution.Width * ScaleFactor.Width); } }
        public int ButtonDimensions { get { return (int)(AppSettings.ButtonDimensions * ScaleFactor.Width); } }
        public int ScreenHeight { get { return (int)(AppSettings.ScreenResolution.Height * ScaleFactor.Height); } }
        public int BaseControlHeight { get { return (int)(AppSettings.BaseControlHeight * ScaleFactor.Height); } }

        public bool ShowMenuButton { get { return m_ShowMenuButton; } set { m_ShowMenuButton = value; } }
        public bool ShowHeader { get { return m_ShowHeader; } set { m_ShowHeader = value; } }
        public bool ShowBackground { get { return m_ShowBackground; } set { m_ShowBackground = value; } }
        public bool EnableRemoteControlGesture { get { return m_EnableRemoteControlGesture; } set { m_EnableRemoteControlGesture = value; } }
        public bool EnableNowPlayingGesture { get { return m_EnableNowPlayingGesture; } set { m_EnableNowPlayingGesture = value; } }
        #endregion

        private void SetBasePositions()
        {
            m_Header.Bounds = new Rectangle(0, 0, ScreenWidth, BaseControlHeight);
            m_MenuButton.Bounds = new Rectangle(5,
                (ScreenHeight - ButtonDimensions),
                ButtonDimensions,
                ButtonDimensions);
            m_MenuButton.Anchor = AnchorBL;

            m_BarManager = null;
            m_BarManager = new BarManager(GetBarOptions());
        }

        protected override void InitControl()
        {
            Bounds = AppSettings.ScreenResolution;

            m_BackgroundImage = ImageFactory.GetBitmap(ImageTypes.Background);

            m_Header = new ClickableLabel();
            m_Header.Text = m_HeaderText;
            m_Header.Font = Theme.CurrentTheme.HeaderFont;
            m_Header.ForeColor = Theme.CurrentTheme.HeaderColor;
            m_Header.LineAlignment = StringAlignment.Center;
            m_Header.Alignment = StringAlignment.Center;
            m_Header.Click += new EventHandler(m_Header_Click);

            m_MenuButton = new FluidButton();
            m_MenuButton.Image = ImageFactory.GetBitmap(ImageTypes.MenuUp);
            m_MenuButton.BackColor = Color.Black;
            m_MenuButton.Corners = Fluid.Drawing.RoundedCorners.Top;
            m_MenuButton.Click += new EventHandler(menuUp_Click);

            SetBasePositions();
            base.InitControl();
            EnableDoubleBuffer = true;
            InitPanelControls();
            SetControlPositions(ScreenOrientation);
            AddControls();

            if (m_ShowHeader)
                Controls.Add(m_Header);
            if (m_ShowMenuButton)
                Controls.Add(m_MenuButton);

            Anchor = AnchorAll;
        }

        void m_Header_Click(object sender, EventArgs e)
        {
            Back();
        }

        public ScreenOrientation ScreenOrientation
        {
            get
            {
                return Width < Height ? ScreenOrientation.Portrait : ScreenOrientation.Landscape;
            }
        }

        protected override void OnPaintBackground(FluidPaintEventArgs e)
        {
            if (m_ShowBackground)
            {
                //base.OnPaintBackground(e);
                Image imageToUse = null;
                if (ScreenOrientation == ScreenOrientation.Landscape)
                {
                    if (m_BackgroundImageLandscape == null)
                    {
                        m_BackgroundImageLandscape = Rotator.RotateImage(270, (Bitmap)m_BackgroundImage);
                    }
                    imageToUse = m_BackgroundImageLandscape;
                }
                else
                {
                    imageToUse = m_BackgroundImage;
                }

                Graphics g = e.Graphics;
                g.DrawImage(imageToUse, this.ClientRectangle, AppSettings.ScreenResolution, GraphicsUnit.Pixel);
            }
        }

        protected override void OnSizeChanged(Size oldSize, Size newSize)
        {
            if (newSize.Height == oldSize.Width && newSize.Width == oldSize.Height)
            {
                Rectangle rect = new Rectangle(UnscaleX(AppSettings.ScreenResolution.X), UnscaleY(AppSettings.ScreenResolution.Y),
                        UnscaleX(newSize.Width), UnscaleY(newSize.Height));
                AppSettings.ScreenResolution = rect;
                base.OnSizeChanged(oldSize, newSize);
                SetBasePositions();
                SetControlPositions(this.ScreenOrientation);
            }
        }

        public override void OnGesture(GestureEventArgs e)
        {
            if (e.Gesture == Gesture.Right && m_EnableRemoteControlGesture)
            {
                e.Handled = true;
                KeyboardPanel.Instance.ShowKeyboard();
                //panel.ShowKeyboard();
            }
            else if (e.Gesture == Gesture.Left && m_EnableNowPlayingGesture)
            {
                e.Handled = true;
                NowPlayingPanel panel = new NowPlayingPanel();//.Instance.ShowNowPlaying();
                panel.ShowNowPlaying();
            }
            else
            {
                base.OnGesture(e);
            }
        }

        public string HeaderText
        {
            set
            {
                m_HeaderText = value;
                if (m_Header != null)
                {
                    m_Header.Text = m_HeaderText;
                }
            }
        }

        public Rectangle AvailableScreen
        {
            get { return new Rectangle(0, m_Header.Height, AppSettings.ScreenResolution.Width, AppSettings.ScreenResolution.Height); }
        }

        public void ShowOptionsBar()
        {
            m_Timer = new Timer();
            m_Timer.Interval = 4000;
            m_Timer.Tick += new EventHandler(m_Timer_Tick);
            m_Timer.Enabled = true;
            m_BarManager.ShowOptionsBar();
        }

        void m_Timer_Tick(object sender, EventArgs e)
        {
            m_BarManager.HideOptionsBar();
            m_Timer.Enabled = false;
        }

        public void HideOptionsBar()
        {
            m_BarManager.HideOptionsBar();
        }

        void menuUp_Click(object sender, EventArgs e)
        {
            ShowOptionsBar();
        }
    }
}
